Ext.define('PJ.view.trainingprogram.Edit', {
	extend: 'AM.default.DefaultWindowForm',
	alias: ['widget.trainingprogramEdit'],
	requires: ['AM.default.DefaultDateField', 
	           'AM.default.DateTime',
	           'AM.default.jsonp.DefaultSubmitJsonp',
	           'AM.default.DefaultDateField',
	           'PJ.view.trainingmanagement.CourseTree',
	           'PJ.view.trainingmanagement.CourseList',//课程列表
	           'PJ.view.trainingprogram.ParticipantsNumList',
	           'PJ.view.trainingprogram.OldParticipantsNumList'
	           ],

	modal: true,// 开启不允许点击其他组件
	frame: true,
	notEdit: true,
	width: 650,
	height:480,
	layout: 'form',
	isSame: false,
	overflowY:'auto',
//	autoScroll:true,
	initComponent: function(){
    	var me = this;
    	var bt = '<span style="color:red;">*</span>';
    	if(me.isReadOnly){
    		bt = ""
    	}
    	var participantsNumListStr = "participantsNumList";
    	if(me.isReadOnly){
    		participantsNumListStr = "oldParticipantsNumList";
    	}
        var theDate = new Date();
        theDate.setFullYear(theDate.getFullYear() + 1, 0, 1);
    	me.items= [{
    		defaults: {
    			labelAlign: 'right',
    			labelWidth: 90,
    			columnWidth: .5,
    			padding:"9 0 0 0",
    			readOnly:me.isView
    		},
    		border: false,
    		xtype: "container",
    		width: 610,
    		//height:470,
    		layout: 'column',
    		allowBlank: false,
    		items: [{
    			xtype: 'textfield',
    			fieldLabel:me.flag==3?'培训班名称': bt+'培训班名称',
    			disabled:me.flag==3?true:false,
    			name: 'classNameNew',
    			allowBlank: me.isReadOnly,
    			readOnly:me.isReadOnly,
    			columnWidth: me.isAdd ? 1:.5,
                listeners: {
                	change: function(textField){
                		if(textField.getValue() && textField.getValue().length > 50){
                			Orchid.alert('提示','该输入项最多输入50个字符！');
                		}
                	}
                },
                maxLength: 50
    		},{
    			xtype: 'textfield',
    			fieldLabel: bt+'计划编号',
    			name: 'applyNumNew',
    			hidden: me.isAdd,
    			readOnly:true,
    			allowBlank: true,
    			disabled:me.flag==3?true:false,
    			emptyText: '请输入10位数字或字母',
    			regex: /^[A-Za-z0-9]{1,10}$/,
    			regexText: '培训班编号只能输入10位长度的数字或字母！'
    		},{
    			xtype: 'textfield',
    			fieldLabel: '承办部委',
    			name: 'undertakeNameNew',
    			allowBlank: true,
    			readOnly:true,
    			margin: "5 0 0 0",
    			value: me.selectScopeStr,
    			disabled:me.flag==3?true:false,
    			columnWidth: .5//me.isReadOnly ? .5 : .44
    		}/*,{
    			xtype: "button",
    			columnWidth:.06,
    			tooltip: '承办部委分类',
    			margin: "9 0 0 5",
    			padding: "4 0 0 3",
    			editable: false,
    			disabled:me.flag==3?true:false,
    			iconCls:"configIco",
    			hidden:me.isReadOnly,
    			//disabled: me.isReadOnly,
    			handler: function(button){
    				Ext.create('AM.default.DefaultWindowView', {
    					title: "选取承办部委",
    					width: 300,
    					height:450,
    					grid:me,
    					items:[{
    						xtype:"trainingTree",
    	                    listeners: {
    	                        // 叶子节点点击事件,如果非叶子节点与此动作相同，需覆盖itemclick事件
    	                        itemClick: function(view, record, htmlelement, index, eventObject, object){
    	                        	if (record.data.id != -1) {
    	                        		eventObject.stopEvent();
    	                        		var values = record.data.text;
//    	                        		var ids = record.data.id;
    	                        		var  baseForm = me.down('form').getForm();
    	                        		var undertakeName = baseForm.findField('undertakeName');
//    	                        		var ministrieID = baseForm.findField('ministrieID');
    	                        		undertakeName.setValue(values);
//    	                        		ministrieID.setValue(ids);
    	                        		this.up('window').close();
    	                        	}
    	                        },
    	                        //显示树的一级菜单
    	                        load: function(treeStore, node, records, successful, eOpts){
    	                            if (records != null && node.hasChildNodes()) {
    	                                this.expandNode(records[0]);
    	                                isExp = false;
    	                            }
    	                        }
    	                    }
    					}]
    				});
    			}
    		}*/,{
    			xtype: 'textfield',
    			fieldLabel:'合办部委',
    			name: 'jointNameNew',
    			allowBlank: true,
    			readOnly:true,
    			margin: '5 0 0 0',
    			disabled:me.flag==3?true:false,
    			columnWidth:me.isReadOnly ? .5 : .38
    		},{
    			xtype: "button",
    			columnWidth:.06,
    			tooltip: '合办部委分类',
    			margin: "13 0 0 5",
    			padding: "3 0 3 3",
    			editable: false,
    			disabled:me.flag==3?true:false,
    			iconCls:"configIco",
    			hidden:me.isReadOnly,
    			//disabled: me.isReadOnly,
    			handler: function(button){
    				Ext.create('AM.default.DefaultWindowView', {
    					title: "选取合办部委",
    					width: 300,
    					height:450,
    					grid:me,
    					items:[{
    						xtype:"trainingTree",
    	                    listeners: {
    	                        // 叶子节点点击事件,如果非叶子节点与此动作相同，需覆盖itemclick事件
    	                        itemClick: function(view, record, htmlelement, index, eventObject, object){
    	                        	if (record.data.id != -1) {
    	                        		eventObject.stopEvent();
    	                        		var values = record.data.text;
    	                        		var ids = record.data.id;
    	                        		var  baseForm = me.down('form').getForm();
    	                        		var jointName = baseForm.findField('jointNameNew');
    	                        		var jointIdNew = baseForm.findField('jointIdNew');
    	                        		jointName.setValue(values);
    	                        		jointIdNew.setValue(ids);
    	                        		this.up('window').close();
    	                        	}
    	                        },
    	                        //显示树的一级菜单
    	                        load: function(treeStore, node, records, successful, eOpts){
    	                            if (records != null && node.hasChildNodes()) {
    	                                this.expandNode(records[0]);
    	                                isExp = false;
    	                            }
    	                        }
    	                    }
    					}]
    				});
    			}
    		},{
    			xtype: "button",
    			columnWidth:.06,
    			tooltip: '清除',
    			margin: "13 0 0 5",
    			padding: "3 0 3 3",
    			editable: false,
    			disabled:me.flag==3?true:false,
    			iconCls:"deleteIco",
    			hidden:me.isReadOnly,
    			//disabled: me.isReadOnly,
    			handler: function(button){
    				var form = button.up("form");
    				var baseForm = form.getForm();
    				baseForm.findField("jointName").setValue("");
    			}
    		}/*,{
    			xtype: 'textfield',
    			fieldLabel: bt+'计划时间',
    			name: 'planDateNew',
    			disabled:me.flag==3?true:false,
    			readOnly:me.isReadOnly,
    			hidden: me.isHideExpectTime,
    			allowBlank: me.isReadOnly
    		}*/, {
				xtype: 'defaultDate',
				fieldLabel: bt + '计划时间',
				name: 'startPlanDateNewStr',
				id: 'startPlanDateNewStr',
				vtype: 'startLessEndDate',
				startLessEndDate: {
					targetCmpId: 'endPlanDateNewStr',
					isStart: true
				},
				margin: '5 0 0 0',
				emptyText: "请输入计划开始时间",
//				disabled:me.isDisable,
				allowBlank: false,
    			readOnly:me.isReadOnly,
    			value: theDate,
                listeners: {
                	change: function(textField){
                		var startPlanDateNewStr = textField.getValue();
                		var startTime = startPlanDateNewStr.getTime();
                		var baseForm = me.down("form").getForm();
                		var endPlanDateNewStr = baseForm.findField("endPlanDateNewStr").getValue();
                		if(endPlanDateNewStr!=null){
                			var endTime = endPlanDateNewStr.getTime();
                			var num = (endTime - startTime)/1000/60/60/24;
                			baseForm.findField("planDayNew").setValue(num+1);
                		}
                	}
                }
			}, {
				xtype: 'defaultDate',
				fieldLabel: bt + '至',
				name: 'endPlanDateNewStr',
				id: 'endPlanDateNewStr',
				vtype: 'startLessEndDate',
				vtypeText: '结束时间不能早于起始时间！',
				startLessEndDate: {
					targetCmpId: 'startPlanDateNewStr',
					isStart: false
				},
				margin: '5 0 0 0',
				emptyText:  me.flag==4?"":"请输入计划结束时间",
//				disabled:me.isDisable,
    			allowBlank: false,
    			readOnly:me.isReadOnly,
    			value: theDate,
                listeners: {
                	change: function(textField){
                		var endPlanDateNewStr = textField.getValue();
                		var endTime = endPlanDateNewStr.getTime();
                		var baseForm = me.down("form").getForm();
                		var startPlanDateNewStr = baseForm.findField("startPlanDateNewStr").getValue();
                		if(startPlanDateNewStr!=null){
                			var startTime = startPlanDateNewStr.getTime();
                			var num = (endTime - startTime)/1000/60/60/24;
                			baseForm.findField("planDayNew").setValue(num+1);
                		}
                	}
                }
			},{
				xtype: 'label',
				html: '<span style="color:red;margin-left:90px;">（不含报到撤离时间）</span>'
			},{
    			xtype: 'numberfield',
    			fieldLabel: bt+'学制(天)',
    			name: 'planDayNew',
    			disabled:me.flag==3?true:false,
    			allowBlank: me.isReadOnly,
    			readOnly:true,
				hideTrigger: true,
				vtype: 'blankTrim',
				minValue:0.1,
				maxValue: 99999,
				decimalPrecision: 1,
				value:1
    		},{
    			xtype: 'textareafield',
    			fieldLabel: bt+'办班目的',
    			name: 'classObjectiveNew',
    			disabled:me.flag==3?true:false,
    			allowBlank: me.isReadOnly,
    			readOnly:me.isReadOnly,
    			columnWidth: 1,
    			height: 46,
    			vtype: 'blankTrim',
                listeners: {
                	change: function(textField){
                		if(textField.getValue() && textField.getValue().length > 150){
                			Orchid.alert('提示','该输入项最多输入150个字符！');
                		}
                	}
                },
                maxLength: 150
    		},{
    			xtype: 'textareafield',
    			fieldLabel: bt+'培训内容',
    			name: 'classValuesNew',
    			disabled:me.flag==3?true:false,
    			allowBlank: me.isReadOnly,
    			readOnly:me.isReadOnly,
    			columnWidth: 1,
    			height: 46,
    			vtype: 'blankTrim',
                listeners: {
                	change: function(textField){
                		if(textField.getValue() && textField.getValue().length > 150){
                			Orchid.alert('提示','该输入项最多输入150个字符！');
                		}
                	}
                },
                maxLength: 150
    		}, {
    			xtype: 'textareafield',
    			fieldLabel: bt+'调训对象',
    			name: 'trainingObjectNew',
    			disabled:me.flag==3?true:false,
    			allowBlank: me.isReadOnly,
    			readOnly:me.isReadOnly,
    			columnWidth: 1,
    			height: 46,
    			vtype: 'blankTrim',
                listeners: {
                	change: function(textField){
                		if(textField.getValue() && textField.getValue().length > 150){
                			Orchid.alert('提示','该输入项最多输入150个字符！');
                		}
                	}
                },
                maxLength: 150
    		}, {
    			xtype: 'textfield',
    			fieldLabel: bt+'培训地点',
    			name: 'trainingAddressNew',
    			columnWidth: 1,
    			vtype: 'blankTrim',
    			readOnly:me.isReadOnly,
    			allowBlank: me.isReadOnly,
                listeners: {
                	change: function(textField){
                		if(textField.getValue() && textField.getValue().length > 150){
                			Orchid.alert('提示','该输入项最多输入150个字符！');
                		}
                	}
                },
                maxLength: 150
    		}, {
    			xtype: 'textfield',
    			fieldLabel: bt+'联系人',
    			name: 'linkManNew',
    			allowBlank: me.isReadOnly,
    			readOnly:me.isReadOnly,
                maxLength: 50
    		}, {
    			xtype: 'textfield',
    			fieldLabel: bt+'联系电话',
    			name: 'linkModeNew',
    			allowBlank: me.isReadOnly,
    			readOnly:me.isReadOnly,
                maxLength: 50
    		}, {
    			xtype: 'textfield',
    			fieldLabel:bt+ '传真',
    			name: 'faxNew',
    			allowBlank: me.isReadOnly,
    			readOnly:me.isReadOnly,
                maxLength: 50
    		}, {
    			xtype: participantsNumListStr,
    			columnWidth: 1,
    			versionState: false
    		},{
    			xtype: 'hidden',
    			allowBlank: true,
    			name: 'planId'//培训班Id
    		},{
    			xtype: 'hidden',
    			allowBlank: true,
    			name: 'jointIdNew'
    		},{
    			xtype: 'hidden',
    			allowBlank: true,
    			name: 'undertakeIdNew'
    		}]
    	}];
		this.buttons = [{
			text:"确定",
			scope:this,
			hidden:me.isReadOnly,
			handler:function(btn){
				var win = me;
				var form = win.down('form');
				var baseForm = form.getForm();
				if (!baseForm.isValid()) {
					return;
				}
				var participantsNumList = win.down("participantsNumList");
				participantsNumList.getSelectionModel().selectAll();
				var records = participantsNumList.getSelectionModel().getSelection();
				var deptIds = new Array();
				var deptNames = new Array();
				var countyPlanCounts = new Array();
				var cityPlanCounts = new Array();
				var planCounts = new Array();
				for(i in records){
					deptIds.push(records[i].get("objectID"));
					deptNames.push(records[i].get("contrastObject"));
					countyPlanCounts.push(records[i].get("contrastCountyValue"));
					cityPlanCounts.push(records[i].get("contrastCityValue"));
					planCounts.push(records[i].get("contrastCountValue"));
				}
				var url = "";
				var params = {
						deptIds: deptIds,
						deptNames: deptNames,
						countyPlanCounts: countyPlanCounts,
						cityPlanCounts: cityPlanCounts,
						planCounts: planCounts
				};
				if(me.flag==1){				//1 新增  2 修改   3 完善
					url = me.grid.store.addUrl;
				}else if(me.flag==2){
					url = me.grid.store.updateUrl;
				}else if(me.flag==3){
					url = me.grid.store.derfectUrl;
					params = {
						className: win.className,
						ministrieName: win.ministrieName,
						deptIds: deptIds,
						deptNames: deptNames,
						countyPlanCounts: countyPlanCounts,
						cityPlanCounts: cityPlanCounts,
						planCounts: planCounts
					};
				}
				
				if(gj_onlineLearning_constant.loadType == "jsonp"){
					baseForm.doAction('submitJsonp',{
						submitEmptyText: false,//忽略emptyValue
						url: url,
						params: params,
						success: function(f, action){
							if (action.result.success) {
								Orchid.alert('消息', "操作成功!", function(){
									var gridPanel = win.grid;
									var store = gridPanel.getStore();
									store.reload();
									win.close();
								});
							}else{
								Orchid.alert('消息', action.result.msg||"系统出错,请联系管理员!", function(){
									win.close();
								});
							}
						},
						failure: function(f, action){
							Orchid.alert('错误', action.result.msg||"系统出错,请联系管理员!");
						}
					});
				}else{
					form.submit({
						submitEmptyText: false,//忽略emptyValue
						url: url,
						params: params,
						success: function(f, action){
							if (action.result.success) {
								Orchid.alert('消息', "操作成功!", function(){
									var gridPanel = win.grid;
									var store = gridPanel.getStore();
									store.reload();
									win.close();
								});
							}else{
								Orchid.alert('消息', action.result.msg||"系统出错,请联系管理员!", function(){
									win.close();
								});
							}
						},
						failure: function(f, action){
							Orchid.alert('错误', action.result.msg||"系统出错,请联系管理员!");
						}
					});
				}
			}
		},{
			text: '关闭',
			scope: this,
			hidden:me.isReadOnly,
			handler: this.close
		},{
			text: '关闭',
			scope: this,
			hidden:!me.isReadOnly,
			handler: this.close
		}];
		this.callParent(arguments);
	}
});